package org.dromara.chat.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.chat.domain.bo.ChatUsageTokenBo;
import org.dromara.chat.domain.vo.ChatUsageTokenVo;
import org.dromara.chat.service.ChatUsageTokenService;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 聊天Token使用记录控制器
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/chat/token")
public class ChatUsageTokenController extends BaseController {

    private final ChatUsageTokenService chatUsageTokenService;

    /**
     * 查询聊天Token使用记录列表
     */
    @SaCheckPermission("chat:token:list")
    @GetMapping("/list")
    public TableDataInfo<ChatUsageTokenVo> list(ChatUsageTokenBo bo, PageQuery pageQuery) {
        return chatUsageTokenService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聊天Token使用记录列表
     */
    @SaCheckPermission("chat:token:export")
    @Log(title = "聊天Token使用记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChatUsageTokenBo bo, HttpServletResponse response) {
        List<ChatUsageTokenVo> list = chatUsageTokenService.queryList(bo);
        ExcelUtil.exportExcel(list, "聊天Token使用记录", ChatUsageTokenVo.class, response);
    }

    /**
     * 获取聊天Token使用记录详细信息
     */
    @SaCheckPermission("chat:token:query")
    @GetMapping("/{id}")
    public R<ChatUsageTokenVo> getInfo(@PathVariable Long id) {
        return R.ok(chatUsageTokenService.queryById(id));
    }

    /**
     * 新增聊天Token使用记录
     */
    @SaCheckPermission("chat:token:add")
    @Log(title = "聊天Token使用记录", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ChatUsageTokenBo bo) {
        return toAjax(chatUsageTokenService.insertByBo(bo));
    }

    /**
     * 修改聊天Token使用记录
     */
    @SaCheckPermission("chat:token:edit")
    @Log(title = "聊天Token使用记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ChatUsageTokenBo bo) {
        return toAjax(chatUsageTokenService.updateByBo(bo));
    }

    /**
     * 删除聊天Token使用记录
     */
    @SaCheckPermission("chat:token:remove")
    @Log(title = "聊天Token使用记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(chatUsageTokenService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 根据用户ID查询Token使用记录列表
     */
    @SaCheckPermission("chat:token:query")
    @GetMapping("/user/{userId}")
    public R<List<ChatUsageTokenVo>> getByUserId(@PathVariable Long userId) {
        return R.ok(chatUsageTokenService.queryListByUserId(userId));
    }

    /**
     * 根据模型名称查询Token使用记录列表
     */
    @SaCheckPermission("chat:token:query")
    @GetMapping("/model/{modelName}")
    public R<List<ChatUsageTokenVo>> getByModelName(@PathVariable String modelName) {
        return R.ok(chatUsageTokenService.queryListByModelName(modelName));
    }

    /**
     * 获取用户总Token使用量
     */
    @SaCheckPermission("chat:token:query")
    @GetMapping("/total/user/{userId}")
    public R<Long> getTotalTokenByUserId(@PathVariable Long userId) {
        return R.ok(chatUsageTokenService.getTotalTokenByUserId(userId));
    }

    /**
     * 获取模型总Token使用量
     */
    @SaCheckPermission("chat:token:query")
    @GetMapping("/total/model/{modelName}")
    public R<Long> getTotalTokenByModelName(@PathVariable String modelName) {
        return R.ok(chatUsageTokenService.getTotalTokenByModelName(modelName));
    }

}